import request from '@/utils/request'
import type { ApiResponse } from '@/types/customer'

// 定义分页响应类型
interface PaginatedResponse<T> {
  list: T[]
  total: number
  page: number
  limit: number
}

// 账号类型定义
interface Account {
  id: number
  customer_name: string
  username: string
  phone: string
  email: string
  status: number
  wechat_bound: boolean
  last_login_time?: number
  create_time: number
}

// 账号统计数据类型
interface AccountStats {
  total: number
  normal: number
  frozen: number
  disabled: number
}

// 查询参数类型
interface AccountQueryParams {
  page?: number
  limit?: number
  customer_name?: string
  phone?: string
  status?: number
  keywords?: string
}

export const accountApi = {
  // 获取账号列表
  getAccountList: (
    params: AccountQueryParams
  ): Promise<ApiResponse<PaginatedResponse<Account>>> => {
    return request({
      url: '/api/customer/account/list',
      method: 'get',
      params,
    })
  },

  // 获取账号统计数据
  getAccountStats: (): Promise<ApiResponse<AccountStats>> => {
    return request({
      url: '/api/customer/account/stats',
      method: 'get',
    })
  },

  // 获取账号详情
  getAccountDetail: (id: number): Promise<ApiResponse<Account>> => {
    return request({
      url: `/api/customer/account/detail/${id}`,
      method: 'get',
    })
  },

  // 创建账号
  createAccount: (data: any): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/customer/account/add',
      method: 'post',
      data,
    })
  },

  // 更新账号
  updateAccount: (id: number, data: any): Promise<ApiResponse<any>> => {
    return request({
      url: `/api/customer/account/edit/${id}`,
      method: 'post',
      data,
    })
  },

  // 删除账号
  deleteAccount: (id: number): Promise<ApiResponse<any>> => {
    return request({
      url: `/api/customer/account/delete/${id}`,
      method: 'post',
    })
  },

  // 冻结账号
  freezeAccount: (id: number): Promise<ApiResponse<any>> => {
    return request({
      url: `/api/customer/account/freeze/${id}`,
      method: 'post',
    })
  },

  // 解冻账号
  unfreezeAccount: (id: number): Promise<ApiResponse<any>> => {
    return request({
      url: `/api/customer/account/unfreeze/${id}`,
      method: 'post',
    })
  },

  // 绑定微信
  bindWechat: (id: number, data: any): Promise<ApiResponse<any>> => {
    return request({
      url: `/api/customer/account/wechat/${id}`,
      method: 'post',
      data,
    })
  },
}
